import { is_empty, uDb, cline } from 'sui';

const load_tech = (setList) => {
    const set = new Set();

    const do_fin = () => {
        const ret = [];

        for (let d of set) {
            ret.push({ sn: d, name: d });
        }

        setList(ret);
    };

    const load_inner = (val) => {
        if (!is_empty(val)) {
            val.forEach(d => set.add(d));
        }

        uDb(null, "field/list/inner", { line: cline() },
            r => {
                r.forEach(d => set.add(d));
                do_fin();
            }, e => do_fin());
    };

    uDb(null, "field/list/outer", { line: cline() }, r => load_inner(r), e => load_inner(null));
};

const set_tp = (v) => {
    if (v.tp === "in") {
        v.tp = "inner";
    } else if (v.tp === "out") {
        v.tp = "outer";
    }
};

const load_wsn = (setMain) => {
    uDb(null, "station/list", { line: cline() },
        r => {
            const ret = {};
            r.forEach(d => ret[d.sn] = d.name);
            setMain(ret);
        }, e => setMain(null));
};

export {load_wsn,  set_tp, load_tech};
